import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/** Main class */
public class QUIRK {
  private static BufferedReader input;
  
  public static void main(String[] args) {
    input = new BufferedReader(new InputStreamReader(System.in));
    try {
      IndexManager mgr = new IndexManager();
      while(true) {
        String command = prompt();
        
        if(command.equals("STATS")) mgr.statsCommand();     
        
        else if(command.equals("INDEX")) mgr.indexCommand();
        
        else if(command.startsWith("QUERY")) { 
          String query = command.substring(command.indexOf("QUERY")+5); 
          //+5 puts the string index after the command
          mgr.queryCommand(query);
        }
        
        else if(command.startsWith("REFINE")) { 
          String query = command.substring(command.indexOf("REFINE")+6); 
          //+6 puts the string index after the command
          mgr.refineCommand(query);
        }
        
        else if(command.equals("SHOW")) mgr.showCommand();  
        
        else if(command.equals("NEXT")) mgr.nextCommand();
        
        else if(command.equals("QUIT")) System.exit(0);
      } 
    }
    catch (IOException ex) {
      System.err.println("There was an error reading the input.");    
    }
    catch (Throwable thr) {
      System.exit(-1);
    }
  }
  
  private static String prompt() throws IOException {
    System.out.print("\n??? ");
    return input.readLine();
  }
}
